﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace PeekService
{
    public class UserMessageService
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(UserMessage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into UserMessage(");
            strSql.Append("Code,Name,Email,[Message],Remark,State,CreateID,CreateTime,Sort)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Name,@Email,@Message,@Remark,@State,@CreateID,@CreateTime,@Sort)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36),
                    new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,20),
					new SqlParameter("@Message", SqlDbType.NVarChar,200),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Email;
            parameters[3].Value = model.Message;
            parameters[4].Value = model.Remark;
            parameters[5].Value = model.State;
            parameters[6].Value = model.CreateID;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.Sort;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UserMessage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update UserMessage set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Email=@Email,");
            strSql.Append("[Message]=@Message,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("State=@State,");
            strSql.Append("CreateID=@CreateID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
                     new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,20),
					new SqlParameter("@Message", SqlDbType.NVarChar,200),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Code", SqlDbType.VarChar,36)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Email;
            parameters[2].Value = model.Message;
            parameters[3].Value = model.Remark;
            parameters[4].Value = model.State;
            parameters[5].Value = model.CreateID;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.Sort;
            parameters[8].Value = model.Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from UserMessage ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserMessage GetModel(string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Code,Name,Email,[Message],Remark,State,CreateID,CreateTime,Sort from UserMessage ");
            strSql.Append(" where Code=@Code  and state in(0,2,3)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            UserMessage model = new UserMessage();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (dr.Read())
                {
                    return DataRowToModel(dr);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有分页
        /// </summary>
        /// <returns></returns>
        public List<UserMessage> GetAll(int pageIndex, int pageSize, out int RecordCount,string name,string email,string message,string state)
        {
            string strWhere = "where State in(0,2,3) ";
            if (!string.IsNullOrEmpty(name))
            {
                strWhere +=" AND Name like '%"+name+"%'";
            }
            if (!string.IsNullOrEmpty(email))
            {
                strWhere += "AND email like '%"+email+"%'";
            }
            if (!string.IsNullOrEmpty(message))
            {
                strWhere += "AND [Message] like '%" + message + "%'";
            }
            if (!string.IsNullOrEmpty(state))
            {
                strWhere += "AND State ="+state;
            }
            string strsql = @"select   Code,Name,Email,[Message],Remark,State,CreateID,CreateTime,Sort from UserMessage "+strWhere+"  order by sort desc";
            List<UserMessage> list = new List<UserMessage>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql, pageIndex, pageSize, out RecordCount))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }


        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<UserMessage> GetAll()
        {
            string strsql = @"select Code,Name,Email,[Message],Remark,State,CreateID,CreateTime,Sort from UserMessage  where state in(0,2,3) order by sort desc";
            List<UserMessage> list = new List<UserMessage>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserMessage DataRowToModel(SqlDataReader row)
        {
            UserMessage model = new UserMessage();
            if (row != null)
            {
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }

                if (row["Name"] != null)
                {
                    model.Name = CommonService.DecodeSQLStr(CommonService.HTMLDecode(row["Name"].ToString()));
                }
                if (row["Email"] != null)
                {
                    model.Email = CommonService.DecodeSQLStr(CommonService.HTMLDecode(row["Email"].ToString()));
                }
                if (row["Message"] != null)
                {
                    model.Message =CommonService.DecodeSQLStr(CommonService.HTMLDecode(row["Message"].ToString()));
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
                if (row["CreateID"] != null)
                {
                    model.CreateID = row["CreateID"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
            }
            return model;
        }
    }
}
